$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var material = "";

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    //点击 返回 箭头
    $(".head .goBack").on("click", function () {
        localStorage.removeItem("work_id");
        window.location.href = "unitewire.html";
    });

    //根据扫描内容 得到 原材料
    $(document).on("keydown", "#goods_id", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 1;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描合绳原料！");
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            loading_show("搜索中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unitewire/getUnitewireMaterial',
                data: {
                    id: localStorage.getItem("work_id"),
                    machine: localStorage.getItem("unitewireMachine"),
                    number: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        var data = res.result;
                        loading_hide();
                        //console.log(data);
                        if ($(".content .list-block .goodsList_ul li[attr-data='" + (data.batch ? data.batch : data.number) + "']").length != 0) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        } else {
                            material = data;
                            var data_html = '<li attr-data="' + data.batch + '"><div class="row no-gutter">' +
                                '<div class="col-90">' +
                                '<p><p><span>编号:</span><span class="sp goods_cpph">' +
                                data.batch + '</span></p><p><span>存货编码:</span><span class="sp goods_cpbm">' +
                                data.inventoryCode + '</span></p><p><span>存货名称:</span><span class="sp goods_cpmc">' +
                                data.inventoryName + '</span></p><p><span>存货规格:</span><span class="sp goods_cpgg">' +
                                data.inventorySpecification + '</span></p><p><span>米长:</span><span class="sp goods_cpmc">' +
                                data.availableLength + '</span></p></div>' +
                                '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                            $(".content .list-block .goodsList_ul").prepend(data_html);
                            $alert("获取成功", 800);
                            badge_text();
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        }
                    } else {
                        pass = 1;
                        loading_hide();
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    //点击 取消 按钮
    $(".foot .quxiao").on("click", function () {
        localStorage.removeItem("work_id");
        window.location.href = "unitewire.html";
    });

    //点击 确定 按钮
    $(".foot .queding").on("click", function () {
        if (!$(".content .goodsList_ul li").length) {
            $alert("请扫描合绳原料！");
            setTimeout(function () {
                $("#goods_id").val("").focus();
            }, 1400);
        } else {
            var numbers = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                numbers.push($(".content .goodsList_ul li").eq(i).find(".row span.goods_cpph").html())
            }
            $.confirm('共:' + $(".content .goodsList_ul li").length + '件，确定首次上料吗？', function () {
                loading_show("首次上料中");
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/unitewire/feedUnitewireMachineFirst',
                    data: {
                        planId: material.plan._id,
                        id: localStorage.getItem("work_id"),
                        machine: localStorage.getItem("unitewireMachine"),
                        numbers: JSON.stringify(numbers)
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            loading_hide();
                            //上料 成功 跳转 页面
                            localStorage.removeItem("work_id");
                            window.location.href = "unitewire.html";
                        } else {
                            loading_hide();
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        loading_hide();
                        console.log(error);
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(error.responseText);
                    }
                });
            });
        }
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        $(".details span.good_length").html(badge_len);
    }

    badge_text();

});
